const acorn = require('acorn')
const walk = require('acorn-walk')

function decompileJs(jsCode) {
    // 1. 解析JS代码
    const ast = acorn.parse(jsCode, {
        ecmaVersion: 2020,
        sourceType: 'module'
    })

    // 2. 提取Vue组件信息
    let componentInfo = {
        template: '',
        script: '',
        style: ''
    }

    walk.simple(ast, {
        CallExpression(node) {
            // 查找Vue组件定义
            if (node.callee.type === 'MemberExpression' &&
                node.callee.property.name === 'push' &&
                node.arguments.length > 1) {
                const componentDef = node.arguments[1]
                extractComponentInfo(componentDef, componentInfo)
            }
        }
    })

    // 3. 格式化输出
    return {
        componentCode: formatComponentCode(componentInfo.script),
        templateCode: componentInfo.template,
        styleCode: componentInfo.style
    }
}
function generateFunctionCode(node) {
    // 生成函数代码
    const params = node.params.map(p => p.name).join(', ')
    const body = node.body.body.map(stmt => generateStatementCode(stmt)).join('\n')
    return `(${params}) => {\n${body}\n}`
}
function generateObjectCode(node) {
    // 生成对象字面量代码
    if (node.type === 'ObjectExpression') {
        const props = node.properties.map(prop => {
            return `${prop.key.name}: ${generateValueCode(prop.value)}`
        })
        return `{ ${props.join(', ')} }`
    }
    return '{}'
}

function extractComponentInfo(node, info) {
    if (node.type === 'ObjectExpression') {
        // 处理webpack打包的Vue组件
        const componentDef = node.properties.find(p => p.key.name === 'default')
        if (componentDef) {
            const componentValue = componentDef.value
            if (componentValue.type === 'ObjectExpression') {
                // 提取data
                const dataProp = componentValue.properties.find(p => p.key.name === 'data')
                if (dataProp) {
                    info.script += `const ${dataProp.value.body.params[0].name} = ${generateObjectCode(dataProp.value.body.body[0].argument)}\n`
                }

                // 提取methods
                const methodsProp = componentValue.properties.find(p => p.key.name === 'methods')
                if (methodsProp) {
                    for (const method of methodsProp.value.properties) {
                        info.script += `function ${method.key.name}${generateFunctionCode(method.value)}\n`
                    }
                }

                // 提取mounted
                const mountedProp = componentValue.properties.find(p => p.key.name === 'mounted')
                if (mountedProp) {
                    info.script += `onMounted(${generateFunctionCode(mountedProp.value)})\n`
                }
            }
        }
    }
}


function extractTemplateFromRender(node) {
    // 简化版：实际实现需要更复杂的AST遍历
    if (node.body.type === 'BlockStatement') {
        for (const stmt of node.body.body) {
            if (stmt.argument && stmt.argument.callee &&
                stmt.argument.callee.name === '_c') {
                return generateTemplateFromVNode(stmt.argument)
            }
        }
    }
    return ''
}

function generateTemplateFromVNode(node) {
    // 简化版：实际实现需要递归处理子节点
    if (node.arguments.length > 1) {
        const tag = node.arguments[0].value
        const attrs = node.arguments[1].properties
        let template = `<${tag}`

        for (const attr of attrs) {
            template += ` ${attr.key.name}="${attr.value.value}"`
        }

        template += '></${tag}>'
        return template
    }
    return ''
}

function formatComponentCode(script) {
    // 添加Vue3的setup语法
    return `<script setup>
${script}
</script>`
}

module.exports = { decompileJs }